<template>
	<view class="conter"  >
		<u-list height="1020rpx" @scrolltolower="scrolltolower">
			<view class="list_duck" v-if="ducklist.length != 0" >
				<u-list-item v-for="(item, index) in ducklist" :key="index">
					<view @click="toggleMask(item,index)">
						<view class="popopop" v-if="selectedItems.some((selectedItem) => selectedItem.id === item.id)">
						</view>
						<view class="min_ducklist">
							<view class="duck_image">
								<!-- 背景图 -->
								<image :src="imgUrl + item.collectionUrl" mode="" class="imgsos">
								</image>
								<view class="number_duck">#{{ item.number }}</view>
							</view>
							<view class="duck_buttom">
								<view class="name_duck">
									<view class="name">
										Duck
									</view>
									<view class="num">繁育次数：{{ item.birthNumber }}/5</view>
								</view>
								<view class="expedition">
									持有
								</view>
							</view>
						</view>
					</view>
				</u-list-item>
			</view>
		</u-list>
		<!-- duck详情 -->
		<view class="qiehaun" v-if="ducklist.length != 0">
			<view class="">
				<image src="../../static/images/crazy/crazyone.png" mode=""></image>
			</view>
			<view class="crazytow">
				<view class="office">
					可上任Duck：{{total}}只
				</view>
				<view class="office">
					已选中Duck：{{ selectNum }}只
				</view>
				<view @click="selectAll" class="offices">
					全选
				</view>
			</view>
			<view @click="startOffice">
				<image class="images" src="../../static/images/crazy/crazytow.png" mode=""></image>
			</view>
		</view>
		
		<view class="bookstore" v-if="ducklist.length == 0">
			<image style="
			width: 280rpx;
			height: 280rpx;
			margin: 105rpx 60rpx 0;
			" src="../../static/images/fighting.gif" mode=""></image>
		</view>
		
	</view>
</template>

<script>
	import {
		getWaiteHuntList,
		pocoHunting,
		getHuntingList
	} from '@/api/breed/crazy.js';
	import {
		toast
	} from '../../utils/common';
	import config from '@/config';
	export default {
		data() {
			return {
				show: true,
				ducklist: [],
				selectNum: 0,
				currentIndex: -1,
				imgUrl: config.gillUrl,
				imgUrl: config.imgUrl,
				selectedItems: [],
				param: {
					page: 1,
					size: 10
				},
				total: 0,
				checked: false,
				extractedIds: ''
			};
		},
		created() {
			this.getMonList();
		},
		methods: {
			//获取可以探险的duck列表
			async getMonList() {
				let res = await getWaiteHuntList(this.param);
				if (res.code == 200) {
					this.ducklist = this.ducklist.concat(res.data.pageData);
					// console.log(this.ducklist);
				}
				this.total = res.data.totalSize
			},
			scrolltolower() {
				//加载时候判断是否还有下一页
				if (this.ducklist.length < this.total) {
					this.param.page++;
					//则还有下一页
					this.status = 'loadmore';
					this.getMonList();
				} else {
					this.param.page = 1;
					this.status = 'nomore';
				}
			},
			// 全选
			selectAll() {
				this.checked = true;
				// 获取所有已加载数据的 ID
				if (this.selectedItems.length === this.ducklist.length) {
					this.selectedItems = [];
					this.checked = false;
				} else {
					// 使用 map 复制数组元素，确保数据引用变化
					this.selectedItems = this.ducklist.map((item) => ({
						...item
					}));
				}
				this.selectNum = this.selectedItems.length;
			},
			isSelected(item) {
				// 判断某个项是否已选中
				return this.selectedItems.some((selectedItem) => selectedItem.id === item.id);
			},
			toggleMask(item, index) {
				this.currentIndex = index;
				if (this.isSelected(item)) {
					// 如果已选中，则从数组中移除
					const index = this.selectedItems.findIndex((selectedItem) => selectedItem.id === item.id);
					if (index !== -1) {
						this.selectedItems.splice(index, 1);
					}
				} else {
					this.selectedItems.push({
						...item
					});
				}
				if (this.selectedItems.length === this.ducklist.length && this.selectedItems.length != 0 && this
					.ducklist.length != 0) {
					this.checked = true;
				} else {
					this.checked = false;
				}
				this.selectNum = this.selectedItems.length;
			},
			startOffice() {
				uni.redirectTo({
					url: '/pages/breeding/crazyDetail?selectedItems=' + JSON.stringify(this.selectedItems)
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.select-box {
		width: 150rpx;
		height: 60rpx;
		padding: 0 20rpx;
		text-align: center;
		position: relative;

		.txt {
			margin-left: 10rpx;
			color: #fff;
			font-size: 24rpx;
			font-family: 'font';
			font-weight: 400;
		}

		image {
			top: 5rpx;
			left: 20rpx;
			width: 26rpx;
			height: 26rpx;
			position: absolute;
			border-radius: 5rpx;
		}
	}

	.conter {
		padding: 10rpx 0;
        .bookstore{
			top: 250rpx;
			left: 180rpx;
			position: absolute;
		}
		.headnavs {
			width: 750rpx;
			height: 112rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: #1c213a;
			.iacon{
				position: absolute;
				left: 34.88rpx;
			}
			.anchor {
				color: #ffffff;
				font-size: 35rpx;
				font-family: "font";
				font-weight: 800;
			}
		}

		.popopop {
			width: 318rpx;
			height: 428rpx;
			border-radius: 10rpx;
			border: 5rpx solid #46A6F8;
			position: absolute;
			z-index: 100;
		}

		.list_duck {
			width: 680rpx;
			display: flex;
			flex-wrap: wrap;
			margin: 0 34.88rpx;
			justify-content: space-between;

			.min_ducklist {
				width: 318rpx;
				height: 408rpx;
				margin-bottom: 45.35rpx;
				border-radius: 9rpx;
				box-shadow: 3rpx 3rpx 5rpx 0rpx rgba(0, 0, 0, 0.15);
			}

			.duck_image {
				position: relative;
				width: 316rpx;
				height: 316rpx;
				border-top: 1rpx solid #3c424d;
				border-left: 1rpx solid #3c424d;
				border-right: 1rpx solid #3c424d;
				border-radius: 9rpx 9rpx 0rpx 0rpx;

				image {
					width: 316rpx;
					height: 316rpx;
					background: #648bd8;
					border-radius: 9rpx 9rpx 0rpx 0rpx;
				}

				.lock {
					width: 316rpx;
					height: 316rpx;
					border-radius: 9rpx 9rpx 0rpx 0rpx;
					position: absolute;
					z-index: 100;
					left: 0;
				}
			}

			.number_duck {
				top: 17.44rpx;
				left: 17.44rpx;
				background: rgba(40, 44, 52, 0.8);
				border-radius: 17rpx;
				position: absolute;
				padding: 5rpx 10rpx 2rpx 10rpx;
				font-family: 'font';
				font-weight: 500;
				color: #46a6f8;
				font-size: 24rpx;
			}

			.duck_buttom {
				width: 316rpx;
				height: 110rpx;
				border-bottom: 1rpx solid #3c424d;
				border-left: 1rpx solid #3c424d;
				border-right: 1rpx solid #3c424d;
				background: #211f30;
				display: flex;
				justify-content: space-between;
				padding: 0 10rpx;
				border-radius: 0rpx 0rpx 9rpx 9rpx;

				.name_duck {
					.name {
						font-size: 24rpx;
						font-family: 'font';
						font-weight: 500;
						color: #ffffff;
						margin: 18rpx 0 0 17rpx;
					}

					.num {
						font-size: 20rpx;
						font-family: 'font';
						font-weight: 500;
						color: #8a92a3;
						margin: 15rpx 0 5rpx 17rpx;
					}
				}

				.expedition {
					width: 90rpx;
					height: 35rpx;
					color: #FFFFFF;
					font-size: 22rpx;
					line-height: 38rpx;
					font-weight: 500;
					margin-top: 55rpx;
					font-family: "font";
					text-align: center;
					background: #46A6F8;
					border-radius: 9rpx;
				}
			}
		}

		.qiehaun {
			width: 680rpx;
			height: 350rpx;
			margin-top: 70rpx;
			border-radius: 14rpx;
			position: relative;
			margin: 85rpx 34.88rpx 0;

			image {
				width: 680rpx;
				height: 350rpx;
			}

			.images {
				top: 160rpx;
				left: 34.88rpx;
				width: 236rpx;
				height: 114rpx;
				position: absolute;
				border-radius: 19rpx;
			}
			
			.crazytow {
				top: 30rpx;
				display: flex;
				width: 680rpx;
				position: absolute;
				padding: 0 20rpx 0 20rpx;
				justify-content: space-between;

				.office {
					color: #ffffff;
					font-size: 24rpx;
					font-weight: 500;
					font-family: 'font';
					border-radius: 19rpx;
					padding: 20rpx;
					background: linear-gradient(270deg, #D46D00 0%, #F3C900 100%);
					box-shadow: 2rpx 2rpx 9rpx 2rpx rgba(134, 69, 1, 0.4);
				}

				.offices {
					color: #ffffff;
					display: flex;
					align-items: center;
					font-size: 24rpx;
					font-weight: 500;
					font-family: 'font';
					border-radius: 19rpx;
					padding: 0 15rpx 0 15rpx;
					background: linear-gradient(270deg, #D46D00 0%, #F3C900 100%);
					box-shadow: 2rpx 2rpx 9rpx 2rpx rgba(134, 69, 1, 0.4);
				}
			}

		}
	}
</style>